package com.zone.vehicle.superApps.view.login.setSecret;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProvider;

import com.zone.sdk.common.http.base.Result;
import com.zone.vehicle.superApps.databinding.FragmentSetSecretBinding;
import com.zone.vehicle.superApps.model.LoginViewModel;
import com.zone.vehicle.superApps.model.ViewModelFactory;
import com.zone.vehicle.superApps.view.common.HandledFragment;

import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;

public class SetSecretFragment extends HandledFragment {

    private FragmentSetSecretBinding binding;

    private LoginViewModel viewModel;

    private static final String ARG_PARAM1 = "param1";


    private String mParam1;

    public SetSecretFragment() {
    }

    public static SetSecretFragment newInstance(String type) {
        SetSecretFragment fragment = new SetSecretFragment();
        Bundle args = new Bundle();
        args.putString(ARG_PARAM1, type);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            mParam1 = getArguments().getString(ARG_PARAM1);
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        binding = FragmentSetSecretBinding.inflate(inflater);
        viewModel = new ViewModelProvider(this, new ViewModelFactory())
                .get(LoginViewModel.class);
        initView();
        return binding.getRoot();
    }

    private void initView(){
        if (StringUtils.isNotEmpty(mParam1)){
            if (mParam1.equals("1")){
                binding.title.setText("");
            }else if (mParam1.equals("2")){

            }
        }
        binding.btnFinish.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
//                requireActivity().finish();
                String secret = binding.secret.getText().toString();
                String reSecret = binding.reSecret.getText().toString();
                if (StringUtils.isNotEmpty(secret)
                        && StringUtils.isNotEmpty(reSecret)
                        && secret.equals(reSecret)){
                    viewModel.userPwdModify(secret);
                }else {
                    showToast("请输入密码并保持一致");
                }
            }
        });

        viewModel.getModifyPwdResult().observe(getViewLifecycleOwner(), new Observer<Result>() {
            @Override
            public void onChanged(Result result) {
                if (ObjectUtils.isNotEmpty(result)
                    && result.getCode() == 200){
                    showToast("密码设置成功");
                    binding.message.setVisibility(View.VISIBLE);
                }
            }
        });
    }

}